package com.example.mybatis_plus_use.controller;

import com.example.mybatis_plus_use.service.CategoryService;
import com.example.mybatis_plus_use.utils.Result; // 假设结果类在 utils 包中
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatis_plus_use.entity.Category;

import java.util.List;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author zjh
 * @since 2025-01-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/category")
@Tag(name = "商品分类表", description = "商品分类表相关接口")
public class CategoryController extends BaseController {
    @Autowired
    @io.swagger.v3.oas.annotations.media.Schema(description = "商品分类服务")
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Operation(summary = "分页查询商品分类")
    @GetMapping("/page")
    public Result<IPage<Category>> getCategoryPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "10") int size) {
        Page<Category> page = new Page<>(current, size);
        IPage<Category> categoryPage = categoryService.page(page, new QueryWrapper<>());
        return Result.success(categoryPage); // 返回分页查询的成功结果
    }

    @Operation(summary = "查询所有商品分类")
    @GetMapping("/list")
    public Result<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.list();
        return Result.success(categories); // 返回查询所有商品分类的成功结果
    }

    @Operation(summary = "根据ID查询商品分类")
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category); // 返回根据ID查询商品分类的成功结果
    }

    @Operation(summary = "新增商品分类")
    @PostMapping
    public Result<Boolean> addCategory(@Validated @RequestBody Category category) {
        boolean isSaved = categoryService.save(category);
        return Result.success(isSaved); // 返回新增商品分类的成功结果
    }

    @Operation(summary = "更新商品分类")
    @PutMapping("/{id}")
    public Result<Boolean> updateCategory(@PathVariable Long id, @Validated @RequestBody Category category) {
        category.setId(id);
        boolean isUpdated = categoryService.updateById(category);
        return Result.success(isUpdated); // 返回更新商品分类的成功结果
    }

    @Operation(summary = "删除商品分类")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Long id) {
        boolean isDeleted = categoryService.removeById(id);
        return Result.success(isDeleted); // 返回删除商品分类的成功结果
    }
}
